{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "538a3f0c-50c1-4952-9fcc-070d365c9a0f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import subprocess\n",
    "from threading import Timer\n",
    "from queue import Queue\n",
    "\n",
    "def is_colab():\n",
    "    try:\n",
    "        import google.colab\n",
    "        return True\n",
    "    except ImportError:\n",
    "        return False\n",
    "        \n",
    "ROOT_DIR = \"/workspace/\" if not is_colab() else \"/content/\"\n",
    "REPO_URL = \"https://huggingface.co/spaces/cagliostrolab/animagine-xl-3.1\"\n",
    "REPO_DIR = os.path.join(ROOT_DIR, \"animagine-xl\")\n",
    "\n",
    "NGROK_TOKEN = \"\"\n",
    "NGROK_SUBDOMAIN = \"\"\n",
    "PORT = 7860\n",
    "\n",
    "# os.environ[\"HF_TOKEN\"] = \"\"\n",
    "os.environ[\"IS_COLAB\"] = \"1\"\n",
    "os.environ[\"MODEL\"] = \"https://huggingface.co/cagliostrolab/animagine-xl-3.1/blob/main/animagine-xl-3.1.safetensors\"\n",
    "os.environ[\"CACHE_EXAMPLES\"] = \"1\"\n",
    "\n",
    "def clone_repository(url, directory, branch=None):\n",
    "    subprocess.run([\"git\", \"clone\", url, directory], check=True)\n",
    "    if branch:\n",
    "        subprocess.run([\"git\", \"checkout\", branch], cwd=directory, check=True)\n",
    "\n",
    "def install_dependencies(directory):\n",
    "    dependencies = [\"accelerate==0.27.2\", \"diffusers==0.26.3\", \"gradio==4.20.0\",\n",
    "                    \"invisible-watermark==0.2.0\", \"spaces==0.24.0\", \"omegaconf==2.3.0\", \"timm==0.9.10\"]\n",
    "    if is_colab():\n",
    "        subprocess.run([\"pip\", \"install\"] + dependencies, check=True)\n",
    "    else:\n",
    "        requirements_path = os.path.join(directory, \"requirements.txt\")\n",
    "        subprocess.run([\"pip\", \"install\", \"-r\", requirements_path], check=True)\n",
    "\n",
    "def setup_ngrok_tunnel(port, queue, auth_token, subdomain):\n",
    "    ngrok.set_auth_token(auth_token)\n",
    "    url = ngrok.connect(port, bind_tls=True, subdomain=subdomain)\n",
    "    queue.put(url)\n",
    "\n",
    "def main():\n",
    "    if not os.path.exists(REPO_DIR):\n",
    "        print(f\"Cloning repository to {REPO_DIR}\")\n",
    "        clone_repository(REPO_URL, REPO_DIR)\n",
    "\n",
    "    print(\"Installing required Python libraries\")\n",
    "    install_dependencies(REPO_DIR)\n",
    "    print(\"Done!\")\n",
    "\n",
    "    os.chdir(REPO_DIR)\n",
    "\n",
    "    if NGROK_TOKEN:\n",
    "        try:\n",
    "            from pyngrok import conf, ngrok\n",
    "        except ImportError:\n",
    "            subprocess.run([\"pip\", \"install\", \"-qqqq\", \"--upgrade\", \"setuptools\"], check=True)\n",
    "            subprocess.run([\"pip\", \"install\", \"-qqqq\", \"-U\", \"pyngrok\"], check=True)\n",
    "            from pyngrok import conf, ngrok\n",
    "\n",
    "        ngrok.kill()\n",
    "        ngrok_output_queue = Queue()\n",
    "        ngrok_thread = Timer(2, setup_ngrok_tunnel, args=(PORT, ngrok_output_queue, NGROK_TOKEN, NGROK_SUBDOMAIN))\n",
    "        ngrok_thread.start()\n",
    "        ngrok_thread.join()\n",
    "        print(ngrok_output_queue.get())\n",
    "\n",
    "    subprocess.run([\"python\", \"app.py\"], check=True)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
